import React from 'react';

import { Route, BrowserRouter as Router, Switch } from 'react-router-dom';

import routes from './routes';
import layouts from './layouts';

const { Login, Home, ShopAudit, User } = routes;
const { UserLayout, BasicLayout } = layouts;

const ROUTES = [
  {
    path: '/login',
    name: '登录',
    layout: 'UserLayout',
    component: Login
  },
  {
    path: '/',
    name: '首页',
    layout: 'BasicLayout',
    icon: 'home',
    component: Home
  },
  {
    path: '/audit',
    name: '审核管理',
    layout: 'BasicLayout',
    icon: 'solution',
    children: [
      { path: '/shop', name: '商户审核', component: ShopAudit },
      { path: '/news', name: '资讯审核', component: Login },
      { path: '/product', name: '商品审核', component: Login }
    ] 
  },
  {
    path: '/user',
    name: '用户管理',
    layout: 'BasicLayout',
    icon: 'user',
    children: [
      { path: '/info', name: '用户信息', component: User },
      { path: '/feedback', name: '用户反馈', component: Login }
    ] 
  }
];

/**
 * 通过Layout取得相关ROUTE
 * @param {*} routes 
 * @param {*} layoutName 
 */
function getRouteData(routes, layoutName) {

  if (!routes.some(item => item.layout === layoutName)) {
    return [];
  }

  return routes.filter(item => item.layout === layoutName);
}

const RouteConfig = () => {

  const routeProps = {
    getRouteData: (layoutName) => getRouteData(ROUTES, layoutName)
  }

  return (
    <Router >
      <Switch>
        <Route path='/login' render={props => <UserLayout {...props} {...routeProps} />} />
        <Route path='/' render={props => <BasicLayout {...props} {...routeProps} />} />
      </Switch>
    </Router>
  );
}

export default RouteConfig;